const App = {
    mixins: [Save],//配置文件，路径/utils/mixins.js
    data() {
        return {
            photo,
            activeName: 'first', // 设置默认激活第一个标签页
            treeData: [],
            form: {},
            village_position: [],
            member: [],
            showDialog: false,
            VillageNumber: '',
            nowVal: 100,
            fileList1: [],
            fileList2: [],
            fileList3: [],
            attachment: [],
            fileUrl: '',
            showDialog1: false,
            isShow:false
        };
    },
    mounted() {
        let ls_URL = window.location.href;
        this.VillageNumber = getQueryObject(ls_URL, "VillageNumber");   // 获取 action 参数
        this.getData();
        this.getList();
    },
    methods: {
        Validity1(rule, value, callback) {
            if (value.filter(i => i.File_Type == '0').length == 0) {
                callback(new Error('请上传会议记录附件'));
            } else {
                callback();
            }
        },
        Validity2(rule, value, callback) {
            if (value.filter(i => i.File_Type == '1').length == 0) {
                callback(new Error('请上传请示批复附件'));
            } else {
                callback();
            }
        },
        Validity3(rule, value, callback) {
            if (value.filter(i => i.File_Type == '2').length == 0) {
                callback(new Error('请上传请示记录附件'));
            } else {
                callback();
            }
        },
        async handleChange({ raw }, type) {
            const result = await uploadBinaryFile(raw);
            if (result.status == 'success') {
                this.form.Children.village_organization_tenure_attachment.push({
                    Attachment_URL: result.fileName,
                    File_Type: type,
                    File_Name: raw.name,
                    Temp_Path: result.tempPath,
                    uid: raw.uid,
                })
            }
        },
        handleRemove({ raw }) {
            this.form.Children.village_organization_tenure_attachment = this.form.Children.village_organization_tenure_attachment.filter(i => i.uid != raw.uid)
        },
        zoomSize(type) {
            if (type == 1) {
                if (this.nowVal == 50) return;
                this.nowVal -= 10;
            } else {
                if (this.nowVal == 300) return;
                this.nowVal += 10;
            }
        },
        getList() {
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/getVillageOrganizationPositionTreeByTenure`,
                type: "GET",
                data: {
                    Tenure_SerialNumber: this.nodeKey,
                },
                success: (data) => {
                    this.treeData = data;
                    this.isShow = false;
                    this.$nextTick(()=>{
                        this.isShow = true;
                    })
                },
                complete: (XMLHttpRequest) => {

                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
        },
        getData() {
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'village_position',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    this.village_position = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {

                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'personal_information',
                    Condition: "Row_Status='0' AND RR_Village_Number LIKE '" + this.VillageNumber + "%'",
                },
                success: (data) => {
                    this.member = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {

                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
        },
        handleNode(node) {
            if (node.ID_Card_Number) {//离任
                console.log(node);
                this.form = {
                    Tenure_SerialNumber: node.Tenure_SerialNumber,
                    Tenure_Position_SerialNumber: node.Tenure_Position_SerialNumber,
                    ID_Card_Number: node.ID_Card_Number,
                    Village_Position_SerialNumber: node.Village_Position_SerialNumber,
                    Actual_End_Date: '',
                    Leave_Type: '',
                    Tenure_Status: '3',
                    Picture_URL: node.Picture_URL,
                    Children: {
                        village_organization_tenure_attachment: [],
                    }
                }
                $.ajax({
                    headers: requestHeaders(),
                    url: `${mh}/generalBusiness/generalQueryByTableName`,
                    type: "POST",
                    data: {
                        Data: 'village_organization_tenure_attachment',
                        Condition: "Row_Status='0' AND Tenure_Member_SerialNumber='" + node.Tenure_Member_SerialNumber + "'",
                    },
                    success: (data) => {
                        this.attachment = data; // 直接使用 this
                    },
                    complete: (XMLHttpRequest) => {

                        requestPretreatment(XMLHttpRequest);
                    },
                    dataType: "json",
                });
            } else {//补选
                this.form = {
                    Tenure_SerialNumber: node.Tenure_SerialNumber,
                    Tenure_Position_SerialNumber: node.Tenure_Position_SerialNumber,
                    ID_Card_Number: '',
                    Village_Position_SerialNumber: node.Village_Position_SerialNumber,
                    Accession_Date: '',
                    Election_Date: '',
                    Tenure_Status: '1',
                    Picture_URL: '',
                    Children: {
                        village_organization_tenure_attachment: [],
                    }
                }
            }
            this.showDialog = true;
        },
        changeMember(val) {
            // let obj = this.member.filter(i => i.ID_Card_Number == val)[0];
            // if (obj) {
            //     this.form.Picture_URL = obj.Picture_URL;
            // }
        },
        async handleSubmit() {
            const allValid = await this.validateAllForms();
            if (allValid) {
                this.loading = true;
                if (this.form.Tenure_Status == '3') {
                    $.ajax({
                        headers: requestHeaders(),
                        url: `${mh}/generalBusiness/queryMultiTableByCondition`,
                        method: "post",
                        data: {
                            Data: JSON.stringify([{
                                tableName: 'village_tenure_member',
                                Condition: window.btoa(unescape(encodeURIComponent(`Tenure_Position_SerialNumber='${this.form.Tenure_Position_SerialNumber}' AND Tenure_Status='1'`))),
                            }, {
                                tableName: 'village_tenure_position',
                                Condition: window.btoa(unescape(encodeURIComponent(`Tenure_Position_SerialNumber='${this.form.Tenure_Position_SerialNumber}'`))),
                            }]),
                        },
                        success: ({ village_tenure_member, village_tenure_position }) => {
                            village_tenure_member[0].Actual_End_Date = this.form.Actual_End_Date;
                            village_tenure_member[0].Leave_Type = this.form.Leave_Type;
                            village_tenure_member[0].Tenure_Status = this.form.Tenure_Status;
                            village_tenure_position[0].ID_Card_Number = '';
                            let village_organization_tenure_attachment = this.form.Children.village_organization_tenure_attachment;
                            village_organization_tenure_attachment.forEach(item => {
                                item.Tenure_Member_SerialNumber = village_tenure_member[0].Tenure_Member_SerialNumber
                            });
                            let lJSONObject_DataSet = {
                                Modify: {
                                    village_tenure_member,
                                    village_tenure_position
                                },
                                Add: {
                                    village_organization_tenure_attachment
                                }
                            }
                            let Operation_Main = JSON.stringify(deepClone(lJSONObject_DataSet));
                            if (!lJSONObject_DataSet['Add']) {
                                lJSONObject_DataSet['Add'] = new Object();
                            }
                            lJSONObject_DataSet['Add']['base_operation_log'] = [{
                                Operation_Type: '0',
                                Operation_Path: this.relativePath,
                                Operation_Main,
                                Operation_Interface: "generalSave",
                                Operation_IP: getIpAddress()
                            }]
                            submitSave(lJSONObject_DataSet, (res) => {
                                if (res.Status == 'success') {
                                    this.showDialog = false;
                                    this.loading = false;
                                    this.getList();
                                }
                            })
                            // $.ajax({
                            //     headers: requestHeaders(),
                            //     url: `${mh}/generalBusiness/generalSave`,
                            //     method: "post",
                            //     data: {
                            //         Data: JSON.stringify(lJSONObject_DataSet),
                            //     },
                            //     success: (res) => {
                            //         if (res.Status == 'success') {
                            //             this.showDialog = false;
                            //             this.getList();
                            //         }
                            //     },
                            //     complete: (XMLHttpRequest) => {

                            //         requestPretreatment(XMLHttpRequest);
                            //     },
                            // });
                        },
                        complete: (XMLHttpRequest) => {

                            requestPretreatment(XMLHttpRequest);
                        },
                    });
                } else if (this.form.Tenure_Status == '1') {
                    $.ajax({
                        headers: requestHeaders(),
                        url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
                        method: "post",
                        data: {
                            Data: 'village_tenure_position',
                            Condition: `Tenure_Position_SerialNumber='${this.form.Tenure_Position_SerialNumber}'`,
                        },
                        success: (data) => {
                            data[0].ID_Card_Number = this.form.ID_Card_Number;
                            let lJSONObject_DataSet = {
                                Add: {
                                    village_tenure_member: [this.form]
                                },
                                Modify: {
                                    village_tenure_position: data
                                }
                            }
                            let Operation_Main = JSON.stringify(deepClone(lJSONObject_DataSet));
                            if (!lJSONObject_DataSet['Add']) {
                                lJSONObject_DataSet['Add'] = new Object();
                            }
                            lJSONObject_DataSet['Add']['base_operation_log'] = [{
                                Operation_Type: '0',
                                Operation_Path: this.relativePath,
                                Operation_Main,
                                Operation_Interface: "generalSave",
                                Operation_IP: getIpAddress()
                            }]
                            submitSave(lJSONObject_DataSet, (res) => {
                                if (res.Status == 'success') {
                                    this.showDialog = false;
                                    this.loading = false;
                                    this.getList();
                                }
                            })
                            // $.ajax({
                            //     headers: requestHeaders(),
                            //     url: `${mh}/generalBusiness/generalSave`,
                            //     method: "post",
                            //     data: {
                            //         Data: JSON.stringify(lJSONObject_DataSet),
                            //     },
                            //     success: (res) => {
                            //         if (res.Status == 'success') {
                            //             this.showDialog = false;
                            //             this.getList();
                            //         }
                            //     }, complete: (XMLHttpRequest) => {

                            //         requestPretreatment(XMLHttpRequest);
                            //     },
                            // });
                        }, complete: (XMLHttpRequest) => {

                            requestPretreatment(XMLHttpRequest);
                        },
                    });
                }
            }
        },
        handlePreview(row) {
            this.fileUrl = this.photo + row.Attachment_URL;
            this.showDialog1 = true;
        },
        goBack() {
            window.history.back();
        },
    },
};

const ylTenureTree = {
    template: `<ul>
    <li :class="isfirst?'part-diagram_root':''" v-for="(item,index) in data" :key="index">
        <div class="picture">
            <div class="child_picture">
                <img :src="item.Picture_URL?photo + item.Picture_URL:'js/nopic.jpg'">
            </div>
        </div>
        <div class="bkuang">
            <span class="subtitle">{{item.Village_Position}}</span>
            <br>
            <span class="nametitle">{{item.Personal_Name}}</span>
            <br>
            <el-button :type="item.ID_Card_Number?'danger':'primary'" @click="handle(item)">
                {{item.ID_Card_Number?'离任':'补选'}}
            </el-button>
        </div>
        <yl-tenure-tree v-if="item.children.length > 0" @node-click="handle" :data="item.children"></yl-tenure-tree>
    </li></ul>`,
    props: {
        data: {
            type: Array,
            default: function () {
                return []
            }
        },
        isfirst: {
            type: Boolean,
            default: false
        }
    },
    emits: ['node-click'],
    data() {
        return {
            photo
        }
    },
    methods: {
        handle(item) {
            this.$emit('node-click', item);
        }
    }
}


const { createApp } = Vue;
const app = createApp(App);
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component);
}
app.component('yl-online-preview', ylOnlinePreview);
app.component('yl-tenure-tree', ylTenureTree);
app.component('yl-upload', ylUpload);
const ElMessage = ElementPlus.ElMessage;
app.use(ElementPlus, { locale: zhCn });
app.mount("#app");
